package com.ruoyi.web.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.html.JsoupUtils;
import com.ruoyi.web.mapper.GxtNoticeMapper;
import com.ruoyi.web.domain.GxtNotice;
import com.ruoyi.web.domain.GxtNoticeFile;
import com.ruoyi.web.domain.dto.GxtNoticeFileDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * description:
 * auth fcb
 * date 2025-11-01 17:53
 **/
@Service
@Slf4j
public class GxtNoticeJsoupService {

    // 工业和信息化厅
    private static final String GOV_LIST_URL = "http://gxt.shandong.gov.cn/col/%s/index.html?uid=%s&pageNum=%s";

    @Autowired
    private GxtNoticeMapper gxtNoticeMapper;
    @Autowired
    private GxtNoticeFileService gxtNoticeFileService;

    public void jsoupGxtNotice(String type, String pageNum) {
        String govListUrl = this.getGovListUrl(type, pageNum);
        Document documentFromURL = JsoupUtils.getDocumentFromURL(govListUrl);
        if(documentFromURL == null) {
            throw new ServiceException("请求"+type+"通知列表页失败");
        }
        Element div586830 = documentFromURL.getElementById("586830");
        if(div586830 == null) {
            throw new ServiceException("无法解析通知列表页面的文章列表");
        }
        Element child_1 = div586830.child(0);
        if(child_1 == null) {
            throw new ServiceException("无法解析政策列表页面的文章列表");
        }
        Document parse = Jsoup.parse(child_1.html());
        Elements recordList = parse.getElementsByTag("record");
        if(CollectionUtils.isEmpty(recordList)) {
            log.info("没有获取到政策列表页文章列表record数据");
            return;
        }
        for(Element record : recordList) {
            try {
                String html = record.html().replace("]]>", "").replace("<![CDATA[", "");
                Document recordDocument = Jsoup.parse(html);
                Elements liList = recordDocument.getElementsByTag("li");
                if (CollectionUtil.isEmpty(liList)) {
                    continue;
                }
                Element li = liList.get(0);
                if (li == null) {
                    continue;
                }
                Elements aList = li.getElementsByTag("a");
                if (CollectionUtil.isEmpty(aList)) {
                    continue;
                }
                Element pElement = li.selectFirst("p");
                if (pElement == null) {
                    continue;
                }
                Elements divBottom = li.getElementsByClass("bottom");
                if (divBottom == null || divBottom.size() != 1) {
                    continue;
                }
                Elements spanList = divBottom.get(0).getElementsByTag("span");
                if (CollectionUtil.isEmpty(spanList) || spanList.size() != 2) {
                    continue;
                }
                Element aElement = aList.get(0);
                String title = aElement.text().trim();
                log.info("获取到文章标题：{}", title);
                String url = aElement.attr("href");
                log.info("获取到文章链接：{}", url);
                // 摘要
                String overview = pElement.text().trim();
                log.info("获取到文章摘要：{}", overview);
                // 发文时间
                String publishTime = spanList.get(0).text().trim();
                log.info("获取到文章发布时间：{}", publishTime);
                // 来源
                String source = spanList.get(1).text().trim();
                log.info("获取到文章来源：{}", source);
                log.info("-------------------------------------------------------------");
                Integer count = gxtNoticeMapper.getCountByTitle(title, type);
                if(count > 0) {
                    log.info("文章已存在，跳过保存");
                    continue;
                }
                GxtNoticeFileDto noticeContentHtml = this.getNoticeContentHtml(url);
                if(noticeContentHtml == null) {
                    continue;
                }
                GxtNotice gxtNotice = new GxtNotice();
                gxtNotice.setBaseId(noticeContentHtml.getNoticeId());
                gxtNotice.setTitle(title);
                gxtNotice.setOverview(overview);
                if(publishTime.length() == 16) {
                    publishTime = publishTime + ":00";
                }
                gxtNotice.setNoticePublishTime(DateUtil.parse(publishTime, "yyyy-MM-dd HH:mm:ss"));
                gxtNotice.setNoticeContent(noticeContentHtml.getNoticeContentHtml());
                gxtNotice.setNoticeType(this.getNoticeType(type));
                gxtNotice.setNoticeUrl(url);
                gxtNotice.setNoticePublisher(source);
                gxtNotice.setBaseCreateBy("system");
                gxtNotice.setBaseCreateTime(new Date());
                gxtNotice.setBaseUpdateBy("system");
                gxtNotice.setBaseUpdateTime(new Date());
                gxtNoticeMapper.insert(gxtNotice);
                if(CollectionUtil.isNotEmpty(noticeContentHtml.getNoticeFileList())) {
                    gxtNoticeFileService.saveBatch(noticeContentHtml.getNoticeFileList());
                }
                Thread.sleep(3000L);
            }catch (Exception e) {
                log.error("解析政策列表页面文章失败：{}", ExceptionUtil.stacktraceToString(e));
            }
        }

    }

    /**
     * 获取html文章内容，去除其他不必要的标签
     * @param url
     * @return
     */
    public GxtNoticeFileDto getNoticeContentHtml(String url) {
        try {
            Document documentFromURL = JsoupUtils.getDocumentFromURL(url);
            if (documentFromURL == null) {
                log.error("获取html文档失败：{}", url);
                return null;
            }
            Elements w1200 = documentFromURL.getElementsByClass("w1200");
            if (CollectionUtil.isEmpty(w1200)) {
                log.error("无法解析html文档：{}", url);
                return null;
            }
            Element w1200element = w1200.get(0);
            if(w1200element == null) {
                log.error("无法解析html文档：{}", url);
                return null;
            }
            GxtNoticeFileDto gxtNoticeFileDto = new GxtNoticeFileDto();
            gxtNoticeFileDto.setNoticeId(IdUtil.simpleUUID());
            Element tree = w1200element.getElementById("tree");
            if(tree != null) {
                tree.remove();
            }
            Elements select = w1200element.select("style");
            if(CollectionUtil.isNotEmpty(select)) {
                for(Element styleElement : select) {
                    styleElement.remove();
                }
            }

            Elements scriptList = w1200element.select("script");
            if (CollectionUtil.isNotEmpty(scriptList)) {
                for (int i = 0; i < scriptList.size(); i++) {
                    scriptList.get(i).remove();
                }
            }

            Elements s17= w1200element.getElementsByClass("s17");

            if(CollectionUtil.isEmpty(s17)) {
                log.error("无法解析html文档：{}", url);
                return null;
            }
            Element s17Element = s17.get(0);
            Element child = s17Element.child(0);
            if(child != null) {
                Elements tables = child.select("table");
                if(CollectionUtil.isNotEmpty(tables)) {
                    for(Element tableElement : tables) {
                        tableElement.remove();
                    }
                    child.remove();
                }
            }

            Elements styleList = s17Element.select("style");
            if(CollectionUtil.isNotEmpty(styleList)) {
                for(Element styleElement : styleList) {
                    styleElement.remove();
                }
            }

            Elements floating = s17Element.getElementsByClass("floating");
            if (CollectionUtil.isNotEmpty(floating)) {
                for (Element floatingElement : floating) {
                    floatingElement.remove();
                }
            }
            // 处理附件连接
            List<GxtNoticeFile> fileList = new ArrayList<>();
            Elements aList = s17Element.select("a");
            if (CollectionUtil.isNotEmpty(aList)) {
                for (Element aElement : aList) {
                    String href = aElement.attr("href");
                    if (StrUtil.isNotEmpty(href) && href.contains("downfile.jsp")) {
                        String fileName = aElement.text();
                        if(StrUtil.isBlank(fileName)) {
                            continue;
                        }
                        String fullUrl = "http://gxt.shandong.gov.cn" + href;
                        aElement.attr("href", fullUrl);
                        aElement.attr("target", "_blank");
                        Elements iconImg = aElement.select("img");
                        if(iconImg != null) {
                            String src = iconImg.attr("src");
                            iconImg.attr("src", "http://gxt.shandong.gov.cn" + src);
                        }
                    }
                }
            }
            gxtNoticeFileDto.setNoticeFileList(fileList);

            // 浏览次数
            Elements headlines = s17Element.getElementsByClass("headline");
            if(CollectionUtil.isNotEmpty(headlines)) {
                Element headlineElement = headlines.get(0);
                Elements labels = headlineElement.select("label");
                if(CollectionUtil.isNotEmpty(labels)) {
                    for(Element labelElement : labels) {
                        if(labelElement.text().contains("浏览次数")) {
                            labelElement.remove();
                        }
                    }
                }
            }

            // 处理图片
            Elements imgList = s17Element.select("img");
            if(CollectionUtil.isNotEmpty(imgList)) {
                for(Element imgElement : imgList) {
                    String src = imgElement.attr("src");
                    if(StrUtil.isNotEmpty(src) && src.startsWith("/picture")) {
                        String fullUrl = "http://gxt.shandong.gov.cn" + src;
                        imgElement.attr("src", fullUrl);
                    }
//                    String style = imgElement.attr("style");
//                    if(StrUtil.isNotEmpty(style)) {
//                        style = style + "width:100%;";
//                        imgElement.attr("style", style);
//                    }else {
//                        imgElement.attr("style", "border:none;width:100%;");
//                    }
                }
            }

            // 处理视频
            Elements videoList = s17Element.select("video");
            if(CollectionUtil.isNotEmpty(videoList)) {
                for(Element videoElement : videoList) {
                    String src = videoElement.attr("src");
                    if(StrUtil.isNotEmpty(src) && src.startsWith("/media")) {
                        String fullUrl = "http://gxt.shandong.gov.cn" + src;
                        videoElement.attr("src", fullUrl);
                    }
                }
            }

            String html = w1200.html().replace("\n", "");
            gxtNoticeFileDto.setNoticeContentHtml(html);
            return gxtNoticeFileDto;
        }catch (Exception e) {
            log.error("处理html文档失败：{}", ExceptionUtil.stacktraceToString(e));
            return null;
        }
    }

    private String getGovListUrl(String type, String pageNum) {
        if("通知".equals(type)) {
            return String.format(GOV_LIST_URL, "col15201", "586830", pageNum);
        }
        if("公告".equals(type)) {
            return String.format(GOV_LIST_URL, "col15202", "586830", pageNum);
        }
        if("公示".equals(type)) {
            return String.format(GOV_LIST_URL, "col15203", "586830", pageNum);
        }
        throw new ServiceException("未知通知类型");
    }

    private Integer getNoticeType(String type) {
        if("通知".equals(type)) {
            return 1;
        }
        if("公告".equals(type)) {
            return 2;
        }
        if("公示".equals(type)) {
            return 3;
        }
        throw new ServiceException("未知通知类型");
    }
}
